package com.cuckoo.cuckoo_app2.apiAsync.Imp;

import android.util.Log;

import com.cuckoo.cuckoo_app2.apiAsync.BaseCallbac;
import com.cuckoo.cuckoo_app2.apiAsync.UserApi;
import com.cuckoo.cuckoo_app2.config.ApiConfig;
import com.cuckoo.cuckoo_app2.utils.NetworkRequestHelperUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class UserApiImp implements UserApi {

    private Gson gson;
    private ApiConfig apiConfig;

    public UserApiImp() {
        this.gson = new Gson();
        this.apiConfig = new ApiConfig();
    }


    @Override
    public void performGetCodeAsync(String key, BaseCallbac<JsonObject> callbac) {
        final String url = "/sys/randomImage/";
        new Thread(new Runnable() {
            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url + key;
                Log.i("HTTP", "GET:" + http);
                try {
                    String response = NetworkRequestHelperUtils.get(http, null);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performGetPhoneCodeAsync(String mobile, String smsmode, BaseCallbac<JsonObject> callbac) {
        final String url = "/sys/sms";
        new Thread(new Runnable() {
            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);

                Map<String, String> body = new HashMap<>();
                body.put("mobile", mobile);
                body.put("smsmode", smsmode);

                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders(body);
                try {
                    String response = NetworkRequestHelperUtils.postJson(http, gson.toJson(body), headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performGetChangePwdCodeAsync(String mobile, BaseCallbac<JsonObject> callbac) {
        final String url = "/sys/sendChangePwdSms";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);
                Map<String, String> body = new HashMap<>();
                body.put("mobile", mobile);
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders(body);
                try {
                    String response = NetworkRequestHelperUtils.postJson(http, gson.toJson(body), headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performRegisterAsync(String userName, String Password, String phone, String smscode, String registerCode, BaseCallbac<JsonObject> callbac) {
        final String url = "/sys/user/register";
        new Thread(new Runnable() {
            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);
                Map<String, String> body = new HashMap<>();
                body.put("username", userName);
                body.put("password", Password);
                body.put("phone", phone);
                body.put("smscode", smscode);
                body.put("registerCode", registerCode);

                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.postJson(http, gson.toJson(body), headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performLoginAsync(String userName, String Password, BaseCallbac<JsonObject> callbac) {
        final String url = "/sys/mLogin";
        new Thread(new Runnable() {
            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);
                Map<String, String> body = new HashMap<>();
                body.put("username", userName);
                body.put("password", Password);

                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders(body);
                try {
                    String response = NetworkRequestHelperUtils.postJson(http, gson.toJson(body), headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performLogoutAsync(BaseCallbac<JsonObject> callbac) {
        final String url = "/sys/logout";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);

                Map<String, String> body = new HashMap<>();
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.postJson(http, gson.toJson(body), headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performGetUserInfoAsync(BaseCallbac<JsonObject> callbac) {
        final String url = "/sys/user/getUserInfo";
        new Thread(new Runnable() {
            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);

                Map<String, String> body = new HashMap<>();
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.get(http, headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performGetVipInfoAsync(String id, BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuUserDetails/queryById";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url + "?id=" + id;
                Log.i("HTTP", "POST:" + http);

                Map<String, String> body = new HashMap<>();
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.get(http, headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performChangePasswordAsync(String userName, String newPassword, String oldpassword, String confirmpassword, BaseCallbac<JsonObject> callbac) {
        final String url = "/sys/user/updatePassword";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);
                Map<String, String> body = new HashMap<>();
                body.put("username", userName);
                body.put("oldpassword", oldpassword);
                body.put("password", newPassword);
                body.put("confirmpassword", confirmpassword);

                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.putJson(http, gson.toJson(body), headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performChangePhoneAsync(String phone, String smscode, BaseCallbac<JsonObject> callbac) {
        final String url = "/sys/user/updateMobile";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);
                Map<String, String> body = new HashMap<>();
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.get(http, headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performSystemFeedbackAsync(String content, String userId, BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuSystemFeedback/add";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                Log.i("HTTP", "POST:" + http);
                Map<String, String> body = new HashMap<>();
                body.put("content", content);
                body.put("userId", userId);
                body.put("start", "0");
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.postJson(http, gson.toJson(body), headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performGetUserByIdAsync(String id, BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuUserDetails/queryById";

        new Thread(new Runnable() {
            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url + "?id=" + id;
                Log.i("HTTP", "GET:" + http);
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.get(http, headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performGetUserListAsync(HashMap<String, String> params, BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuUserDetails/list";
        new Thread(new Runnable() {

            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                http += "?" + NetworkRequestHelperUtils.mapToUrlParams(params);

                Log.i("HTTP", "GET:" + http);
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.get(http, headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }

    @Override
    public void performGetWithdrawalInformationAsync(String type, BaseCallbac<JsonObject> callbac) {
        final String url = "/cuckoo/cuWithdrawalInformation/queryByUser";
        new Thread(new Runnable() {
            @Override
            public void run() {
                String http = apiConfig.API_URL + apiConfig.API_PREFIX + url;
                http += "?type=" + type;
                Log.i("HTTP", "GET:" + http);
                Map<String, String> headers = NetworkRequestHelperUtils.getHeaders();
                try {
                    String response = NetworkRequestHelperUtils.get(http, headers);
                    Log.i("HTTP", "response:" + response);
                    JsonObject jsonObject = gson.fromJson(response, JsonObject.class);
                    callbac.onSuccess(jsonObject);
                } catch (IOException e) {
                    callbac.onFailure("服务器请求失败");
                } catch (Exception e) {
                    callbac.onFailure("应用执行错误");
                }
            }
        }).start();
    }
}
